/*
 *  RunRunGame.c
 *  final
 *
 *  Created by Javier Quevedo on 1/21/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#import "RunRunGame.h"
#import "Scene.h"
#import "Dino.h"
#import "Locust.h"
#import "Node.h"
#import "Map.h"

#define characterRotationPeriod 1000.0

RunRunGame *initRunRunGame(Camera *camera){
	RunRunGame *newGame = malloc(sizeof(Scene));
	
	newGame->cat=initCat();
	//newGame->dino=initDino();
	newGame->locust=initLocust();
	newGame->camera=camera;
	
	newGame->map=initMap();	
	//setDinoSize(newGame->dino, 0.1);
	setCatSize(newGame->cat, 0.1);
	setLocustSize(newGame->locust, 0.1);
	
	newGame->scene=initScene();
	//addNodeToScene(newGame->scene, newGame->dino->main);
	addNodeToScene(newGame->scene,newGame->cat->main);
	addNodeToScene(newGame->scene,newGame->locust->main);
	addNodeToScene(newGame->scene, newGame->map->main);
	newGame->timestamp=0.0;
	
	
	setGameSelectChar(newGame);
	


	
	

	
	return newGame;
	
}

updateGamePlay(RunRunGame *game, GLint currentTime){

}
updateGameSelectChar(RunRunGame *game, GLint currentTime){
	GLfloat angleDif = (((360.0 / (GLfloat) characterRotationPeriod)) * (GLfloat) (currentTime - game->timestamp) );
	Node *nodeToRotate = game->scene->sceneNodes[game->currentCharacter];
	nodeToRotate= nodeToRotate->leftChild;
	GLfloat x = 0.0;
	GLfloat y = 0.0;
	GLfloat z = 0.0;
	
	switch (game->currentCharacter) {
		case 0:
			z = 1.0;
			break;
		case 1:
			y = 1.0;
			break;
		case 2:
			x = 1.0;
			break;
	}	
	
	//game->scene->sceneNodes[game->currentCharacter]; 
	rotateNode(nodeToRotate, angleDif,x, y, z);
	//transLateNode(game->scene->sceneNodes[game->currentCharacter], 0.0, 0.0, 0.01);
}
updateGameEnd(RunRunGame *game, GLfloat currentTime){}


void setGameSelectChar(RunRunGame *game){
	game->state=DS_SelectChar;
	game->currentCharacter=CC_Cat;
	setCamera(game->camera, 0.0, 1.0, -3.0, 0.0, 1.0, 0.0);
	//transLateNode(game->dino->main, 1.0, 0.0, 0.0);
	transLateNode(game->cat->main, 1.0, 0.0, 0.0);
	transLateNode(game->locust->main, -1.0, 0.0, 0.0);
	
	
}

void updateGame(RunRunGame *game, GLint currentTime){
	if (game->state==DS_Play)
		updateGamePlay(game, currentTime);
	else if (game->state==DS_SelectChar)
	{
		updateGameSelectChar(game, currentTime);
	}
	else 
		updateGameEnd(game, currentTime);
	
	game->timestamp = currentTime;
}	

void renderGame(RunRunGame *game){
	renderScene(game->scene);
	}

void releaseRunRunGame(RunRunGame *game){
	releaseScene(game->scene);
	releaseDino(game->dino);
	releaseCat(game->cat);
	free(game);
}

selectCharMoveLeft(RunRunGame *game, GLubyte key){
	if (game->currentCharacter > 0)
	{
		game->currentCharacter--;
	}
	
	else return;
}

selectCharMoveRight(RunRunGame *game, GLubyte key){
	if (game->currentCharacter < 1)
	{
		game->currentCharacter++;
	}
	
	else return;
	
}

void setGamePlay(RunRunGame *game){
	game->state=DS_Play;
	switch (game->currentCharacter) {
		case 0:
		transLateNode(game->cat->main, -1.0, 0.0, 0.0);
		break;
		case 1:
			transLateNode(game->locust->main, 1.0, 0.0, 0.0);
		default:
			break;
	}
	
	}


void gameKeyboardEventHandlerSelectChar(RunRunGame *game, GLubyte key)
{
	
	switch (key)
	{                                                         
		case 'a': // scape key
		{
			selectCharMoveLeft(game, key);
			break;
		}
		case 'd': // scape key
		{
			selectCharMoveRight(game, key);
			break;
		}
		case 13: // scape key
		{
			setGamePlay(game);
		}
		default:
			break;
	}
}

void gameKeyboardEventHandler(RunRunGame *game, GLubyte key){
	if (game->state == DS_SelectChar)
		gameKeyboardEventHandlerSelectChar(game, key);
}
